﻿using UGUIEffect.UIParticles;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;

namespace UIParticles.Editor
{
    /// <summary>
    /// Custom editor for UI Particles component
    /// </summary>
    [CustomEditor(typeof(UGUIEffect.UIParticles.UIParticles))]
    public class UiParticlesEditor : GraphicEditor
    {

        private SerializedProperty m_RenderMode;
        private SerializedProperty m_StretchedSpeedScale;
        private SerializedProperty m_StretchedLenghScale;
        private SerializedProperty m_MeshUseParticlesColor;
        private SerializedProperty m_IgnoreTimescale;
        private SerializedProperty m_Scale;
        private SerializedProperty m_SkinnedMesh;
        private SerializedProperty m_MFMesh;
        private SerializedProperty m_StaticRefresh;

        protected override void OnEnable()
        {
            base.OnEnable();

            m_RenderMode = serializedObject.FindProperty("m_RendererMode");
            m_StretchedSpeedScale = serializedObject.FindProperty("m_StretchedSpeedScale");
            m_StretchedLenghScale = serializedObject.FindProperty("m_StretchedLenghScale");
            m_MeshUseParticlesColor = serializedObject.FindProperty("m_MeshUseParticleColor");
            m_IgnoreTimescale = serializedObject.FindProperty("m_IgnoreTimescale");
            m_Scale = serializedObject.FindProperty("m_Scale");
            m_SkinnedMesh = serializedObject.FindProperty("m_SkinndMesh");
            m_MFMesh = serializedObject.FindProperty("m_MFilterMesh");
            m_StaticRefresh = serializedObject.FindProperty("m_StaticRefresh");
        }


        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            UGUIEffect.UIParticles.UIParticles uiParticleSystem = (UGUIEffect.UIParticles.UIParticles) target;

            if (GUILayout.Button("Apply to nested particle systems"))
            {
                var nested = uiParticleSystem.gameObject.GetComponentsInChildren<ParticleSystem>();
                foreach (var particleSystem in nested)
                {
                    if (particleSystem.GetComponent<UGUIEffect.UIParticles.UIParticles>() == null)
                        particleSystem.gameObject.AddComponent<UGUIEffect.UIParticles.UIParticles>();
                }
            }

            EditorGUILayout.PropertyField(m_RenderMode);

            if (uiParticleSystem.RenderMode == UIParticleRenderMode.StreachedBillboard)
            {
                EditorGUILayout.PropertyField(m_StretchedSpeedScale);
                EditorGUILayout.PropertyField(m_StretchedLenghScale);
            }

            if (uiParticleSystem.RenderMode == UIParticleRenderMode.Mesh)
            {
                EditorGUILayout.PropertyField(this.m_MeshUseParticlesColor);
                EditorGUILayout.PropertyField(this.m_SkinnedMesh);
                EditorGUILayout.PropertyField(this.m_MFMesh);
                GUIStyle labStyle = new GUIStyle();
                labStyle.normal.textColor = Color.red;
                labStyle.fontStyle = FontStyle.Bold;
                EditorGUILayout.LabelField("如果不是改变顶点的特效！请勾选 Static Refresh!！", labStyle);
                EditorGUILayout.PropertyField(this.m_StaticRefresh);
            }


            EditorGUILayout.PropertyField(m_IgnoreTimescale);
            EditorGUILayout.PropertyField(m_Scale);

            serializedObject.ApplyModifiedProperties();
        }
    }
}
